import dotenv from 'dotenv';
import Koa from 'koa';

import { Server } from 'http';

import accessLogMiddleware from './middleware/accessLogMiddleware';
import koaBody from 'koa-body';
import { catchError } from './middleware/catchError';
import apiRouter from './router';
//import { ResponseMiddleware } from './middleware/responseMiddleware';
import errors from './exception/http-exception';
import errorHandler from './utils/errHandler';
import './sequelize/index';

dotenv.config();

global.errs = errors;
const app = new Koa();
//.use(catchError)
app.use(koaBody()).use(accessLogMiddleware).use(catchError).use(apiRouter.routes());

const run = (port: number): Server => {
  console.log('监听的端口号：', port);
  console.log('访问地址为：', 'localhost:' + port);
  return app.listen(port);
};
app.on('error', errorHandler);
export default run;
